﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
public partial class Manager_OrderApp_OrderApp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {
            txtTenSP.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 

            if (Request.QueryString["Status"] == "N" && Session["dtBasketAdmin"] != null)//tao don hang hoan toan moi
                Session.Remove("dtBasketAdmin");
           BindTopicList();
            BindCatalogueByTopicID();
 

            //Bind lbxRight neu ben trang dat hang nhan nut quay lai
            if (Session["dtBasketAdmin"] != "" && Session["dtBasketAdmin"] != null)
            {
                DataTable dt = (DataTable)Session["dtBasketAdmin"];
                lbxRight.DataSource = dt;
                lbxRight.DataTextField = "PdName";
                lbxRight.DataValueField = "PDID";
                lbxRight.DataBind();
            }
            BinData();
        }
    }
  
    private void BindTopicList()
    {
       /* clsWebShop cls = new clsWebShop();
   
        ddlChuDe.DataSource = cls.Get_TopicNoneProc().Tables[0];
        ddlChuDe.DataTextField = "TopicName";
        ddlChuDe.DataValueField = "TopicID";
        ddlChuDe.DataBind();*/
        ListItem item = new ListItem();
        item.Text = "-- Chọn chủ đề --";
        item.Value = "0";
        ddlChuDe.Items.Insert(0, item);
       // FindValueDLL(ddlChuDe, Request.QueryString["sTopicID"]);
    }

   

    private void BindCatalogueByTopicID()
    {
      /*  clsWebShop cls = new clsWebShop();

       
        DataTable dt = null;
        dt = cls.Get_CatalogueByTopicIDNoneProc(ddlChuDe.SelectedValue).Tables[0];
        ddlTheLoai.DataSource = dt;
        ddlTheLoai.DataTextField = "Catname";
        ddlTheLoai.DataValueField = "CatID";
        ddlTheLoai.DataBind();*/
        ListItem item = new ListItem();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlTheLoai.Items.Insert(0, item);
        //FindValueDLL(ddlTheLoai, Request.QueryString["sCatID"]);
    }

    private void FindValueDLL(DropDownList ddl, string value)
    {
        if (value != "" && value != null)
            ddl.Items.FindByValue(value).Selected = true;
    }
    private void BinData()
    {
        //Bind lbxLeft
        string sExceptPDR = "";
        if (lbxRight.Items.Count > 0)
        {
            for (int i = 0; i < lbxRight.Items.Count; i++)
            {
                sExceptPDR += "'" + lbxRight.Items[i].Value.Trim() + "',";
            }
            sExceptPDR = sExceptPDR.Substring(0, sExceptPDR.Length - 1);
        }
        PccMsg msg = new PccMsg();
    
        msg.CreateFirstNode("PdName", txtTenSP.Text.Trim());
        msg.CreateFirstNode("CatID", ddlTheLoai.SelectedValue);
        msg.CreateFirstNode("TopicID", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("KindNo", ddlType.SelectedValue);  
        msg.CreateFirstNode("ExceptPD", sExceptPDR);
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_ExceptProductByCondition(msg.GetXmlStr);
        lbxLeft.DataSource = ds;
        lbxLeft.DataTextField = "PdName";
        lbxLeft.DataValueField = "PDID";
        lbxLeft.DataBind();

    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public DataSet AjaxGetPD(string sPDName, string sTopic, string sCatID, string sExceptPDR)
    {
        PccMsg msg = new PccMsg();

        msg.CreateFirstNode("PdName", sPDName.Trim());
        msg.CreateFirstNode("CatID", sCatID);
        msg.CreateFirstNode("TopicID", sTopic);
        msg.CreateFirstNode("ExceptPD", sExceptPDR);
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_ExceptProductByCondition(msg.GetXmlStr);
        return ds;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCatalogueByTopicID();
        BinData();
    }
    protected void btnINext_Click(object sender, ImageClickEventArgs e)
    {
        if (lbxLeft.SelectedIndex == -1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Chọn sản phẩm cho vào giỏ hàng')</script>");
            return;
        }
        for (int i = 0; i < lbxLeft.Items.Count; i++)
        {
            if (lbxLeft.Items[i].Selected == true)
            {
                ListItem item = new ListItem();
                item.Text = lbxLeft.Items[i].Text;
                item.Value = lbxLeft.Items[i].Value;
                lbxRight.Items.Add(item);
            }
        }
        txtTenSP.Text = "";
        BinData();
    }

    protected void btnIPre_Click(object sender, ImageClickEventArgs e)
    {
        if (lbxRight.SelectedIndex == -1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Chọn sản phẩm cho ra khỏi giỏ hàng')</script>");
            return;
        }
        for (int i = 0; i < lbxRight.Items.Count; i++)
        {
            if (lbxRight.Items[i].Selected == true)
            {
                lbxRight.Items.Remove(lbxRight.Items[i]);
                i--;
            }
        }
        txtTenSP.Text = "";
        BinData();
    }
    protected void btnILast_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < lbxLeft.Items.Count; i++)
        {
            ListItem item = new ListItem();
            item.Text = lbxLeft.Items[i].Text;
            item.Value = lbxLeft.Items[i].Value;
            lbxRight.Items.Add(item);
        }
        BinData();
    }
    protected void btnIfirst_Click(object sender, ImageClickEventArgs e)
    {
        lbxRight.Items.Clear();
        BinData();
    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {

        ArrayList arrOPd = new ArrayList();
        for (int i = 0; i < lbxRight.Items.Count; i++)
        {
            arrOPd.Add(lbxRight.Items[i].Value);
        }
        if (lbxRight.Items.Count <= 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('Chọn sản phẩm cho vào giỏ hàng')</script>");
            return;
        }

        Session["arrOPd"] = arrOPd;
        Response.Redirect("BasketApp.aspx?CodeID=" + Request.QueryString["CodeID"]);
        // ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.fnOrderApp();</script>");
        //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.fnOrderApp();</script>");
    }
    protected void btnCancelBasket_Click(object sender, EventArgs e)
    {
        Session["arrOPd"] = null;
        lbxRight.Items.Clear();
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    protected void ddlKindNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
    }
}